$OpenBSD: patch-radicale_auth_htpasswd_py,v 1.2 2014/12/15 13:27:13 ian Exp $

bcrypt support, based on
http://evilshit.wordpress.com/2013/11/19/how-to-install-a-caldav-and-carddav-server-using-radicale/#bcrypt

--- radicale/auth/htpasswd.py.orig	Sun Aug  3 17:51:47 2014
+++ radicale/auth/htpasswd.py	Sun Nov 16 19:01:49 2014
@@ -30,6 +30,7 @@ supported, but md5 is not (see ``htpasswd`` man page t
 import base64
 import hashlib
 import os
+import bcrypt
 
 from .. import config
 
@@ -59,11 +60,21 @@ def _sha1(hash_value, password):
     return sha1.digest() == base64.b64decode(hash_value)
 
 
+def _bcrypt(hash_value, password):
+    """Check if ``hash_value`` and ``password`` match using bcrypt method."""
+    hash_value = hash_value.encode("ascii")
+    password = password.encode(config.get("encoding", "stock"))
+    return bcrypt.checkpw(password, hash_value)
+
+
 def is_authenticated(user, password):
     """Check if ``user``/``password`` couple is valid."""
     for line in open(FILENAME).readlines():
         if line.strip():
             login, hash_value = line.strip().split(":")
             if login == user:
-                return globals()["_%s" % ENCRYPTION](hash_value, password)
+                if hash_value[0:5] == '{SHA}':
+                    return _sha1(hash_value, password)
+                else:
+                    return globals()["_%s" % ENCRYPTION](hash_value, password)
     return False
